﻿namespace Mp3Observer.Common.Language
{
    #region usings

    using System;
    using System.Globalization;
    using System.Windows.Data;
    using Mp3Observer.Common.IoC;
    using Mp3Observer.Interfaces;

    #endregion

    /// <summary>
    /// This converter gets the string from the resource by calling the IResourceAccess Interface
    /// </summary>
    [ValueConversion(typeof(string), typeof(string))]
    public sealed class LanguageConverter : IValueConverter
    {
        #region Fields

        /// <summary>
        /// name of the resource
        /// </summary>
        private string name;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="LanguageConverter"/> class.
        /// </summary>
        /// <param name="name">The name of the resource</param>
        public LanguageConverter(string name)
        {
            this.name = name;
        }

        #endregion

        #region IValueConverter Members

        /// <summary>
        /// To Convert gets the String by the IResourceAccess Interface
        /// </summary>
        /// <param name="value">value is not used, the input for the converter comes from the constructor</param>
        /// <param name="targetType">targetType it not used</param>
        /// <param name="parameter">parameter it not used</param>
        /// <param name="culture">culture it not used</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        /// <exception cref="ArgumentException">if resource key is not found</exception>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string resourceValue = IoCFactory.Resolve<IResources>().GetString(this.name);
            if (resourceValue.IsNullOrEmpty())
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "Resource Key '{0}' not foun.!", this.name));
            return resourceValue;
        }

        /// <summary>
        /// Convert back does nothing but must be implemented
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Binding.DoNothing;
        }

        #endregion
    }
}
